home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 820 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  4.8 KB

  1. From: Roman Lechtchinsky <wolfro@cs.tu-berlin.de>
  2. Message-ID: <3151E556.5995@cs.tu-berlin.de>
  3. X-Original-Date: Fri, 22 Mar 1996 00:25:10 +0100
  4. Path: in1.uu.net!bounce-back
  5. Date: 22 Mar 96 01:48:38 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Return-Path: <daemon@migs.UCAR.EDU>
  8. Newsgroups: comp.std.c++
  9. Subject: Re: Constructors and conversion operator
  10. Organization: Technical University of Berlin
  11. References: <31505061.2E03@cs.tu-berlin.de> <4is5ao$dds@engnews1.Eng.Sun.COM>
  12. X-Mailer: Mozilla 2.0 (Win95; I)
  13. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  14.     iQBFAgUBMVIHqOEDnX0m9pzZAQHfYQF/YDjmlWdCDMPBbaizCoAz2VQgpnPORefk
  15.     Wemm+4IalfCBPi3Y/EWGOHrEM0vsjn44
  16.     =HP4P
  17.  
  18. Steve Clamage wrote:
  19. >
  20. > In article 2E03@cs.tu-berlin.de, Roman Lechtchinsky
  21. > <wolfro@cs.tu-berlin.de> writes:
  22. > >Steve Clamage wrote:
  23. > >>
  24. > >> Suppose a copy constructor T::T(const T&) is viewed as a conversion
  25. > >> from T to T. If such a conversion is "needed", the shortest conversion
  26. > >> sequence must be chosen. That would be the null sequence, and so the
  27. > >> copy ctor would never be used or even considered for that purpose.
  28. > >> (Just as the sequence int->Rational->float would never be considered in
  29. > >> converting an int to a float.)
  30. > >
  31. > >I'm not too sure here. First, if the copy constructor is the identity
  32. > >conversion, why is it called at all? I mean, if the identity conversion is
  33. > >eliminated from the conversion sequence, why isn't the call to the copy
  34. > >constructor itself eliminated? Second, if the copy constructor is called,
  35. > >why is it called only once? Consider
  36. > >
  37. > >class A { A(const A&); };
  38. > >void foo(A);
  39. > >A a;
  40. > >
  41. > >Now, foo(a) is implicitly converted to foo(A(a)). Why not to foo(A(A(a))?
  42. >
  43. > Let's not confuse type conversion with making a copy.
  44.  
  45. No, no, no... Not me, the draft does (IMO). Anyway, this question is a bit
  46. off topic, I think. I'll try to explain this once more.
  47.  
  48. Part I.
  49.  
  50. This is what the DWP says about converting constructors:
  51.  
  52. 1) A constructor declared without the  function-specifier  explicit  that
  53.   can  be called with a single parameter specifies a conversion from the
  54.   type of its first parameter to the type of its  class.   Such  a 
  55.   constructor is called a converting constructor. [class.conv.ctor]
  56.  
  57. 2) A  nonconverting  constructor  constructs objects just like converting
  58.   constructors, but does so only where a constructor call is  explicitly
  59.   indicated by the syntax. [class.conv.ctor]
  60.  
  61. This is what the DWP says about copy constructors:
  62.  
  63.   A constructor for class X is a copy constructor if its first parameter
  64.   is of type X& or const X& and either there are no other parameters  or
  65.   else  all other parameters have default arguments. [class.copy]
  66.  
  67. This means that per definitionem a copy constructor is a converting
  68. constructor and thus a user-defined conversion. Note that nothing is said
  69. about the types that participate in the conversion. In particular, it is
  70. *not* said that the identity conversion is not a user-defined conversion.
  71. Thus, everything the DWP says about user-defined conversions applies to
  72. copy constructors.
  73. This is what the DWP says about user-defined conversions:
  74.  
  75.   At most one user-defined conversion (constructor or  conversion 
  76.   function) is implicitly applied to a single value. [class.conv.fct]
  77.  
  78. As you can see, the DWP doesn't even talk about converting types; it
  79. mentions only applying user-defined conversions.
  80. Now, let's return to my example:
  81.  
  82. class A { A(const A&); };
  83. class B { operator A&(); };
  84. void foo( A );
  85. B b;
  86.  
  87. The call foo(b) requires that
  88.  
  89. a) the conversion operator B::A&() be applied to b;
  90. b) the converting constructor A::A(const A&) be applied to the value returned
  91. by the conversion operator B::A&().
  92.  
  93. Which makes two user-defined conversions implicitly applied to the single
  94. value b. Hence the call is illegal.
  95.  
  96. Part II.
  97.  
  98. Now, let's see if the copy constructor is the identity conversion. The copy
  99. constructor takes an argument of type A&, not A ( a constructor like A(A) is
  100. illegal ). Look at the definition of converting constructors and substitute
  101. "the type of its parameter" with A& and "the type of its class" with A. The
  102. copy constructor specifies a conversion from A& to A. AFAIK, A& and A are
  103. still different types. To me, this doesn't look like an identity conversion.
  104.  
  105. Well, the way you see this is certainly the way it should be. However, even
  106. if I'm missing something, this all is rather unclear and the main advantage
  107. of a standard is (should be) clarity. Thus, IMO the standard should contain
  108. an explicit statement on this topic.
  109.  
  110. Bye
  111.  
  112. Roman
  113. ---
  114. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  115. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  116. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  117. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  118. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  119.